package main

import (
	//"gouser/common"
	common "git.mashibing.com/msb_47094/shopping-comm"
	"gouser/domain/repository"
	"gouser/domain/service"
	"gouser/handler"
	"gouser/proto"
	"log"
	"time"

	consul "github.com/asim/go-micro/plugins/registry/consul/v4"
	"go-micro.dev/v4"
	"go-micro.dev/v4/registry"
)

func main() {
	//0 配置中心
	consulConfig, err := common.GetConsulConfig(common.ConsulStr, common.UserFileKey)
	if err != nil {
		log.Println("consulConfig err :", err)
	}
	// 1.consul注册中心
	//注册到consul
	consulReg := consul.NewRegistry(func(options *registry.Options) {
		options.Addrs = []string{common.ConsulReistStr}
	})
	repcService := micro.NewService(
		micro.RegisterTTL(time.Second*30),
		micro.RegisterInterval(time.Second*30),
		micro.Name("shop-user"),
		micro.Address(":8081"),
		micro.Version("v1"),
		micro.Registry(consulReg),
	)
	//2.初始化db   & redis
	//mysql
	db, _ := common.GetMysqlFromConsul(consulConfig)
	//redis
	consulRedisConfig, err := common.GetConsulConfig(common.ConsulStr, common.RedisFileKey)
	red, _ := common.GetRedisFromConsul(consulRedisConfig)
	//测试 缓存
	//common.SetUserToken(red, "uuid1111", []byte("tokenxxx"), time.Duration(1)*time.Hour)
	//val := common.GetUserToken(red, "uuid1111")
	//fmt.Println(">>>>>>>>>>>>>>>>>>>>> ", val)
	//3.创建服务实例
	userDataService := service.NewUserDataService(repository.NewUserRepository(db, red))
	//4.注册handler
	proto.RegisterLoginHandler(repcService.Server(), &handler.User{userDataService})
	proto.RegisterGetUserTokenHandler(repcService.Server(), &handler.User{userDataService})
	//5.启动服务
	if err := repcService.Run(); err != nil {
		log.Println("start user service err :", err)
	}

}
